<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------

/**
 * 功能: 处理用户请求有跨域需求的 session 与cookie的取值，必须通过这个方法
 * 创建日期:Wed Oct 29 08:52:36 CST 2008
 * 最后更新:
 * 作者: sanshi 
 */
/**
 * 对user使用cookie或者session封装
 * @name          UserRequest
 * @version	1.0.0 
 * @author	sanshi
 */
class UserRequest
{
	/**
	 * 构造 方法
	 * @access public
	 * */
	public function UserRequest()
	{
		if(REQUEST_TYPE=="session")
		{
			self::user_session();
		}
	}
	public function __construct()
	{
		$this->UserRequest();
	}
	/**
	 * 强制使用session
	 * @name user_session
	 * @return  void
	 * */
	public static  function user_session()
	{
		//通过工厂类的方法做适配，具体的适配去工厂类里整理
		Factory::getSession();
	}
	/**
	 * 使用session设置变量
	 * @name   user_set_session
	 * @param  string	param_name 	参数名
	 * @param  string   param_value 参数值
	 * @return void 
	 * @access public
	 * @see 	  
	 * @version	1.0.0 (Wed Oct 29 16:19:20 CST 2008)
	 * @author	sanshi	 
	 */
	public static function  user_set_session ($param_name,$param_value)
	{
		$_SESSION[$param_name] = $param_value;
	}
	/**
	 * 取得session的值
	 * @name   user_get_session
	 * @param  string	param_name 	取得的参数名
	 * @return string 	
	 * @access public
	 * @see 	  
	 * @version	1.0.0 (Wed Oct 29 16:24:00 CST 2008)
	 * @author	sanshi	 
	 */
	public static function user_get_session ($param_name)
	{
		return isset($_SESSION[$param_name]) ? $_SESSION[$param_name] : "";
	}
	/**
	 * 设置用户使用cookie
	 * @name   user_set_cookie
	 * @param  string	param_name 	参数名
	 * @return void 		
	 * @access public
	 * @see 	  
	 * @version	1.0.0 (Wed Oct 29 16:26:46 CST 2008)
	 * @author	sanshi	 
	 */
	public static function user_set_cookie ($param_name,$param_value,$cookie_time)
	{
		setcookie($param_name, $param_value,time()+$cookie_time,'/',COOKIE_DOMAIN);
	}
	/**
	 * 取得用户的cookie
	 * @name   user_get_cookie
	 * @param  string	param_name 	取得的参数
	 * @return string 	
	 * @access public
	 * @see 	  
	 * @version	1.0.0 (Wed Oct 29 16:30:33 CST 2008)
	 * @author	sanshi	 
	 */
	public static function user_get_cookie ($param_name)
	{
		return isset($_COOKIE[$param_name])?$_COOKIE[$param_name]:"";
	}
	/**
	 * 取得用户想要的信息
	 * @name get
	 * @param string  要取得的参数
	 * @return string 返回参数对应的值
	 * */
	public static  function get($param_name)
	{
		if(REQUEST_TYPE=="session")
		{
			//print_r($_SESSION);
			return isset($_SESSION[$param_name]) ? $_SESSION[$param_name] : "";
		}else{
			return isset($_COOKIE[$param_name])?$_COOKIE[$param_name]:"";
		}
	}
	/**
	 * 设置值
	 * @name  get
	 * @param string $param_name  设置参数名
	 * @param string $param_value 参数值
	 * @param int    $cookie_time 设置cookie有效时间 默认为3600秒，对于session无效
	 * @return  void
	 * */
	public static  function set($param_name,$param_value,$cookie_time=3600)
	{
		if(REQUEST_TYPE=="session")
		{
			$_SESSION[$param_name] = $param_value;
		}else{
			setcookie($param_name, $param_value,time()+$cookie_time,'/',COOKIE_DOMAIN);
		}
	}
	/**
	 * 释放一个变量
	 * @name  free
	 * @param string $param_name  释放变量下标
	 * @return void
	 * */
	public static  function free($param_name)
	{
		if(REQUEST_TYPE=="session")
		{
			unset($_SESSION[$param_name]);
		}else {
			setcookie($param_name, $param_value,time()-3600,'/',COOKIE_DOMAIN);
		}
	}
}
?>